#parse('include.java')
#set($sp = $CSU.getSuperPackage($model.package,1))
#if($sp)
package ${sp}.dao;
#end

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import $!{model.package}.${model.name};

@Repository
public class ${model.name}DAO extends BasicBizDAO<${model.name},Long>{

#set($refFieldLst = $MU.getReferenceFields($model))
#foreach($field in $refFieldLst)
#set($methodName = 'findBy'+$CSU.getHumpWord($field.name))
	public List<${model.name}> ${methodName}($field.type $field.name) {
		return this.findListByProperty("$field.name", $field.name);
	}
#end

	public List<${model.name}> pageQuery(Page page #refParams($model)) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
#trim('eachrow', 2, 'rn')
		#set($refFieldLst = $MU.getReferenceFields($model))
		#foreach($field in $refFieldLst)
			if($field.name != null){
				criteria.add(Restrictions.eq("$field.name", $field.name));
			}
		#end
#end
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<${model.name}> pageQuery(Page page#allParams($model)) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
#trim('eachrow', 2, 'rn')
		#set($strFields = $MU.findField($model, "type='String'"))
		#set($idName = 'id')
		#set($allFields = $MU.getSortedList($model.fields))
		#foreach($field in $allFields)
			#if($field.type=='Date')
				if(${field.name}Start != null){
					criteria.add(Restrictions.ge("$field.name", ${field.name}Start));
				}
				if(${field.name}End != null){
					criteria.add(Restrictions.le("$field.name", ${field.name}End));
				}
			#elseif(!$field.id && $MU.isPrimitive($field.type))
				if($field.name != null){
					criteria.add(Restrictions.eq("$field.name", $field.name));
				}
			#end
			#if($field.id)
				#set($idName = $field.name)
			#end
		#end

		#if($strFields && $strFields.size()>0)
			if(keyword!=null && !keyword.isEmpty()){
				#if($strFields.size()==1)
					criteria.add(Restrictions.like("$strFields.get(0).name", keyword, MatchMode.ANYWHERE).ignoreCase());
				#elseif($strFields.size()==2)
					LogicalExpression expr = Restrictions.or(
							Restrictions.like("$strFields.get(0).name", keyword, MatchMode.ANYWHERE).ignoreCase(),
							Restrictions.like("$strFields.get(1).name", keyword, MatchMode.ANYWHERE).ignoreCase()
					);
					criteria.add(expr);
				#else
					LogicalExpression expr = Restrictions.or(
						Restrictions.like("$strFields.get(0).name", keyword, MatchMode.ANYWHERE).ignoreCase(),
						Restrictions.like("$strFields.get(1).name", keyword, MatchMode.ANYWHERE).ignoreCase()
					);
					#foreach($strField in $strFields)
						#if($foreach.count>2)
							expr = Restrictions.or(expr, Restrictions.like("$strField.name", keyword, MatchMode.ANYWHERE).ignoreCase());
						#end
					#end
					criteria.add(expr);
				#end
			}
		#end

#end
		criteria.addOrder(Order.desc("$idName"));
		return this.findByCriteria(criteria, page);
	}

#*
	public List<${model.name}> pageQueryList(Page page){
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}
*#

#if($MU.isRelationModel($model))
	public $TYPE findItem(#allParams($model, true, false)) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
#trim('eachrow', 2, 'rn')
		#foreach($field in $model.fields)
			#if(!$field.id)
				criteria.add(Restrictions.eq("${field.name}", ${field.name}));
			#end
		#end
#end
		return this.findUniqueByCriteria(criteria);
	}
#end
}
